﻿@{
    ViewBag.Title = "用户管理";
    Layout = "~/Views/Shared/_EasyUILayout.cshtml";
}

<script type="text/javascript">
    $(function () {
        loadOrgs();
        loadUsers();
    });

    function loadOrgs() {
        $('#treOrg').tree({
            url: '@Url.Action("Data", "Org", new { area = "Admin" })',
            idField: 'id',
            treeField: 'text',
            onClick: function (node) {
                $('#dg').datagrid({
                    queryParams: { orgCode: node.Code }
                });
            }
        });
    }

    //加载数据
    function loadUsers() {
        $('#dg').datagrid({
            url: '@Url.Action("Data")',
            fit: true,//自动填充
            singleSelect: true,//单选
            pagination: true,//分页信息
            rownumbers: true,//显示行号
            pageNumber: 1,//默认显示第一页
            pageSize:20,//每页的行数
            idField: 'UserID',
            columns: [[

                { field: 'Name', title: '姓名', width: 120, halign: 'center', align: 'center', sortable: true },
                { field: 'OrgName', title: '隶属机构', width: 350, halign: 'center', align: 'left', sortable: true },
                { field: 'IDCard', title: '身份证号', width: 200, halign: 'center', align: 'left', sortable: true },
                { field: 'Email', title: '电子邮件', width: 200, halign: 'center', align: 'left', sortable: true },
                { field: 'SexName', title: '性别', width: 60, halign: 'center', align: 'center', sortable: true },
                { field: 'Mobile', title: '手机号码', width: 120, halign: 'center', align: 'center', sortable: true },
                { field: 'Account', title: '帐号', width: 120, halign: 'center', align: 'center', sortable: true }
            ]],
            rowStyler: function (index, row) {
                if (row.State == 0) {
                    return 'color:red';
                }
            },
            onDblClickRow: function () {
                editInfo();
            },
            queryParams: { state: '1' }
        });
    }

    //工具栏按钮单击事件
    function toolbarClick(key) {
        switch (key) {
            case "Add":
                addInfo();
                break;
            case "Edit":
                editInfo();
                break;
            case "Delete":
                deleteInfo();
                break;
            case "Enable":
                enableInfo();
                break;
            case "Disable":
                disableInfo();
                break;
            case "ResetPwd":
                resetPwd();
                break;
        }
    }

    //添加信息
    function addInfo() {
        var node = $('#treOrg').tree('getSelected');
        var orgId = node == null ? '' : node.id;
        var orgCode = node == null ? '' : node.Code;

        common.showDialog('@Url.Action("Add")?orgId=' + orgId + '&orgCode=' + orgCode, '添加用户', 900, 550, function () {
            $('#dg').datagrid('reload');
        });
    }

    //修改信息
    function editInfo() {
        var row = $('#dg').datagrid('getSelected');
        if (row == null) {
            common.alert('请先选择一条记录。');
            return;
        }

        common.showDialog('@Url.Action("Edit")?id=' + row.UserID, '修改用户', 600, 500, function () {
            $('#dg').datagrid('reload');
        });
    }

    //删除信息
    function deleteInfo() {
        var row = $('#dg').datagrid('getSelected');
        if (row == null) {
            common.alert('请先选择一条记录。');
            return;
        }

        var postData = new Object();
        postData.id = row.UserID;
        common.antiToken(postData);

        common.confirm('你确认要删除[<span class="msg_tip">' + row.Name + '</span>]吗?', function () {
            $.post('@Url.Action("Delete")', postData, function (result) {
                common.processResult(result, function () {
                    $('#dg').datagrid('reload');
                });
            });
        });
    }

    //启用信息
    function enableInfo() {
        var row = $('#dg').datagrid('getSelected');
        if (row == null) {
            common.alert('请先选择一条记录。');
            return;
        }

        var postData = new Object();
        postData.id = row.UserID;
        common.antiToken(postData);

        common.confirm('你确认要启用[<span class="msg_tip">' + row.Name + '</span>]吗?', function () {
            $.post('@Url.Action("Enable")', postData, function (result) {
                common.processResult(result, function () {
                    $('#dg').datagrid('reload');
                });
            });
        });
    }

    //禁用信息
    function disableInfo() {
        var row = $('#dg').datagrid('getSelected');
        if (row == null) {
            common.alert('请先选择一条记录。');
            return;
        }

        var postData = new Object();
        postData.id = row.UserID;
        common.antiToken(postData);

        common.confirm('你确认要禁用[<span class="msg_tip">' + row.Name + '</span>]吗?', function () {
            $.post('@Url.Action("Disable")', postData, function (result) {
                common.processResult(result, function () {
                    $('#dg').datagrid('reload');
                });
            });
        });
    }

    //重设密码
    function resetPwd() {
        var row = $('#dg').datagrid('getSelected');
        if (row == null) {
            common.alert('请先选择一条记录。');
            return;
        }

        var postData = new Object();
        postData.id = row.UserID;
        common.antiToken(postData);

        common.confirm('你确认要重设[<span class="msg_tip">' + row.Name + '</span>]的密码吗?', function () {
            $.post('@Url.Action("ResetPwd")', postData, function (result) {
                common.processResult(result);
            });
        });
    }

    //查询
    function query() {
        var node = $('#treOrg').tree('getSelected');
        var orgCode = node == null ? '' : node.Code;

        $('#dg').datagrid('unselectAll');
        $('#dg').datagrid({
            queryParams: {
                state: $('#txtState').combobox('getValue'),
                orgCode: orgCode,
                keyword: $('#txtKeyword').val()
            }
        });
    }
</script>
@Html.AntiForgeryToken()
<div data-options="region:'center',border:false">
    <div class="easyui-layout" data-options="fit:true" style="width: 100%; height: 100%">
        <div data-options="region:'north',border:false">
            <div class="datagrid-toolbar">
                @Html.Toolbar()
            </div>
            <div class="query">
                <table>
                    <tr>
                        <td>状态</td>
                        <td><input type="text" id="txtState" class="easyui-combobox" data-options="data: [{value: '',text:'--全部--'},{value: '1',text:'启用'},{value: '0',text:'禁用'}],valueField:'value',textField:'text',editable:false,panelHeight:'auto',value:'1'" style="width:90px" /></td>
                        <td>快速查询</td>
                        <td>
                            <input id="txtKeyword" type="text" class="easyui-textbox enterQuery" data-options="prompt:'姓名、拼音首字母、手机号或帐号'" style="width: 240px" />
                        </td>
                        <td>
                            <a class="easyui-linkbutton" id="btnQuery" onclick="query()" data-options="iconCls:'icon-search'">查询</a>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
        <div data-options="region:'center',border:false">
            <div class="easyui-layout" data-options="fit:true" style="width: 100%; height: 100%">
                <div data-options="region:'west',border:true,split:true,collapsible:true" style="width:220px">
                    <table id="treOrg" style="width:100%"></table>
                </div>
                <div data-options="region:'center',border:false">
                    <table id="dg"></table>
                </div>
            </div>
        </div>
    </div>
</div>
